﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using PeekService;

namespace PeekWebApp.Tester
{
    public partial class UploadFileTest : System.Web.UI.Page
    {
        private List<FileModel> fileList = new List<FileModel>();
        protected void Page_Load(object sender, EventArgs e)
        {
            this.btnUpload.Click += new EventHandler(btnUpload_Click);
            this.dlImg.ItemCommand += new DataListCommandEventHandler(dlImg_ItemCommand);
           // this.dlImg.ItemDataBound += new DataListItemEventHandler(dlImg_ItemDataBound);

        }

        void dlImg_ItemCommand(object source, DataListCommandEventArgs e)
        {
            
        }

        //void dlImg_ItemDataBound(object sender, DataListItemEventArgs e)
        //{
        //    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        //    {
        //        string str = e.Item.DataItem as string;
        //        Image img = e.Item.FindControl("imgitem") as Image;
        //        if (img != null)
        //        {
        //            img.ImageUrl = this.ResolveUrl("~/img/" + str);
        //        }

        //    }
        //}

        void btnUpload_Click(object sender, EventArgs e)
        {
            FileModel fileModel = new FileModel();
            string fileNameSuo = this.fudSmall.FileName;
            string path = "~/images/thumbnails/" + fileNameSuo;
            if (File.Exists(Server.MapPath(path)))
            {
                File.Delete(Server.MapPath(path));
            }            
            fudSmall.SaveAs(Server.MapPath(path));
            fileModel.Id = Guid.NewGuid().ToString();
            fileModel.Small = path;
            string fileNormal = this.fudNormal.FileName;
            string pathNormal = "~/images/normal/"+fileNormal;
            if (File.Exists(Server.MapPath(pathNormal)))
            {
                File.Delete(Server.MapPath(pathNormal));
            }
            fudNormal.SaveAs(Server.MapPath(pathNormal));
            fileModel.Normal = pathNormal;
            string fileBig = this.fudBig.FileName;
            string pathBig = "~/images/preview/"+fileBig;
            if (File.Exists(Server.MapPath(pathBig)))
            {
                File.Delete(Server.MapPath(pathBig));
            }
            fudBig.SaveAs(Server.MapPath(pathBig));
            fileList.Add(fileModel);
            BindImg();
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            //TourismInfo tinfo = new TourismInfo();
            //tinfo.Code = Guid.NewGuid().ToString();
            //tinfo.CreateTime = DateTime.Now;
            //foreach (string str in hidImg.Value.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries))
            //{
            //    SysFile sysFile = new SysFile();
            //    sysFile.Code = Guid.NewGuid().ToString();
            //    sysFile.BusinessCode = tinfo.Code;
            //    sysFile.CreateTime = DateTime.Now;

            //}
        }

        /// <summary>
        /// 绑定图片方法
        /// </summary>
        private void BindImg()
        {
            this.dlImg.DataSource = fileList;
            dlImg.DataBind();

        }
    }
}